<template>
  <div>
    <excel-list
      :mem-initdatas-add="initDatas"
      :disabled="updatable === false"
      ref="frameList"
      :main-service="mainService"
      :key="childListService"
      :col-srv="childListService"
      :service-name="service"
      :data="item"
      :type="listType"
      :default-condition="getDefaultConditions"
      :childForeignkey="foreignKey"
      :main-data="mainData"
      :div-cond="divCond"
      :is-tree="isTree"
      v-if="
        uiMode === 'excel' &&
        // 'addchildlist' === listType &&
        service
      "
    ></excel-list>
    <treegrid
      :disabled="updatable === false"
      :read-only="updatable === false || readOnly"
      :mainService="mainService"
      ref="list"
      :name="name"
      list-type="treelist"
      :listMainFormDatas="mainFormDatas"
      :storage-type="storageType"
      :service="service"
      :page-size="pageSize"
      :childForeignkey="foreignKey"
      :default-condition="getDefaultConditions"
      :main-data="mainData"
      :div-cond="divCond"
      :memInitdatasAdd="initDatas"
      @list-loaded="onListLoaded()"
      @add-form-loaded="onAddFormLoaded($event)"
      @update-form-loaded="onUpdateFormLoaded($event)"
      v-else-if="isTree && storageType === 'db'"
    >
    </treegrid>

    <list-proc
      ref="list"
      :service="service"
      :disabled="updatable === false"
      v-else-if="isProc"
      :div-cond="divCond"
    >
    </list-proc>
    <tableEdit
      ref="mlist"
      :name="name"
      :disabled="updatable === false"
      :pageType="foreignKey.view_model"
      :pageIsDraft="pageIsDraft"
      :showPagination="childListConfig.showPagination && showPagination"
      :list-type="listType"
      :defaultCondition="defaultCondition"
      :storage-type="storageType"
      :service="service"
      :childForeignkey="foreignKey"
      :def-data-para="defDataPara"
      :read-only="readOnly"
      :childforeignvalue="getRefColValue"
      :default-condition="getDefaultConditions"
      :inplace-edit="inplaceEdit"
      :default-inplace-edit-mode="defaultInplaceEditMode"
      :default-dirty-flags="defaultDirtyFlags"
      :merge-col="mergeCol"
      :listMainFormDatas="mainFormDatas"
      :main-data="mainData ? mainData : formModel"
      :$srvApp="$srvApp"
      :div-cond="divCond"
      @child-loaded="childDataLoadedRun($event)"
      @list-loaded="onListLoaded"
      @inline-list-loaded="onInlineListLoaded"
      @add-form-loaded="onAddFormLoaded($event)"
      @update-form-loaded="onUpdateFormLoaded($event)"
      @duplicate-form-loaded="onAddFormLoaded($event)"
      @filter-form-loaded="onFilterFormLoaded"
      @list-data-loaded="listLoaded($event)"
      @grid-data-changed="$emit('grid-data-changed', $event)"
      @standby-row-added="onStandbyRowAdded"
      v-else-if="foreignKey.view_model == 'mlist'"
    ></tableEdit>
    <tab-list
      ref="list"
      :key="service"
      :tab-list-type="listType"
      :storage-type="storageType"
      :service="service"
      :foreignKey="foreignKey"
      :default-condition="getDefaultConditions"
      :inplace-edit="inplaceEdit"
      :default-inplace-edit-mode="defaultInplaceEditMode"
      :default-dirty-flags="defaultDirtyFlags"
      :$srvApp="$srvApp"
      :div-cond="divCond"
      :list-main-form-datas="mainFormDatas"
      :mainService="mainService"
      :list-name="service"
      :childforeignvalue="getRefColValue"
      :def-data-para="defDataPara"
      :readOnly="readOnly"
      :memInitdatasAdd="initDatas"
      @child-loaded="childDataLoadedRun($event)"
      @list-loaded="onListLoaded"
      @inline-list-loaded="onInlineListLoaded"
      @add-form-loaded="onAddFormLoaded($event)"
      @update-form-loaded="onUpdateFormLoaded($event)"
      @duplicate-form-loaded="onAddFormLoaded($event)"
      @filter-form-loaded="onFilterFormLoaded"
      @list-data-loaded="listLoaded($event)"
      @grid-data-changed="$emit('grid-data-changed', $event)"
      @standby-row-added="onStandbyRowAdded"
      v-else
    >
    </tab-list>
  </div>
</template>

<script>
import SimpleAdd from "./simple-add.vue";
import SimpleUpdate from "./simple-update.vue";
import List from "./list.vue";
// import TabList from "./tab-list2.vue";
import Treegrid from "./treegrid.vue";
import ListProc from "./listproc.vue";
import ChildListMixin from "../mixin/child-list-mixin";

import tableEdit from "./table-edit.vue";
import excelList from "../feature/excel-list.vue"; // 新增的excel表格

import { evalJson } from "@/util/evalJsonExpr.js";

import concat from "lodash/concat";
/**
 * 子表组件， 主要是处理外键相关的逻辑： 例如外籍列、外键disp列隐藏；添加行数据自动添加外键列的值。
 */
export default {
  components: {
    excelList,
    Treegrid,
    SimpleUpdate,
    SimpleAdd,
    List,
    TabList: () => import("./tab-list2.vue"),
    ListProc,
    tableEdit,
  },

  mixins: [ChildListMixin],

  data() {
    return {
      pageSize: 5,
    };
  },
  props: {
    item: Object,
    name: {
      type: String,
      default: "child",
    },
    isTableForm: false,
    mainFormDatas: {
      type: Object,
      default: function () {
        return null;
      },
    },
    service: {
      type: String,
    },
    foreignKey: {
      type: Object,
    },
    isTree: {
      //子表是树形表
      type: Boolean,
      default: false,
    },
    isProc: {
      //子表是流程表
      type: Boolean,
      default: false,
    },
    defDataPara: {
      type: Object,
      default: function () {
        return null;
      },
    },
    readOnly: {
      type: Boolean,
      default: function () {
        return false;
      },
    },
    defaultCondition: {
      type: Array,
      default: function () {
        return [];
      },
    },
    searchForm: {
      type: Boolean,
      default: function () {
        return true;
      },
    },
    storageType: {
      type: String,
      default: "db",
    },

    inplaceEdit: {
      type: Boolean,
      default: false,
    },

    listType: {
      type: String,
      default: "detaillist",
    },

    defaultInplaceEditMode: {
      type: Boolean,
      default: false,
    },

    defaultDirtyFlags: {
      type: String,
      default: "pristine",
    },

    mergeCol: {
      type: Boolean,
      default: true,
    },
    childListConfig: {
      type: Object,
      default: function () {
        return {};
      },
    },
    pageIsDraft: {
      type: String,
      default: "norm",
    },
    mainData: {
      type: Object,
      default: function () {
        return {};
      },
    },
    mainService: {
      type: String,
      default: "",
    },
    divCond: Array,
  },
  computed: {
    isInplaceEditRun: function () {
      if (
        this.foreignKey &&
        this.foreignKey.hasOwnProperty("more_config") &&
        this.foreignKey.more_config
      ) {
        let config = JSON.parse(this.foreignKey.more_config);
        if (config.tableType == "special") {
          return config;
        } else {
          return null;
        }
      } else {
        return null;
      }
    },
    getRefColValue: function () {
      if (this.defaultCondition && this.defaultCondition.length > 0) {
        if (typeof this.defaultCondition[0].value === "function") {
          return this.defaultCondition[0].value(this.mainFormDatas);
        }
        return this.defaultCondition[0].value;
      }

      return null;
    },

    innerList: function () {
      if (this.$refs.list && this.$refs.list.$refs.list) {
        return this.$refs.list.$refs.list;
      } else {
        return this.$refs.list;
      }
      return this.$refs?.list?.$refs?.list || this.$refs?.list;
    },

    gridData: function () {
      return this.getInnerList?.()?.gridDataRun;
    },

    inlineLists: function () {
      return this.getInnerList?.()?.inlineLists;
    },

    getDefaultConditions: function () {
      if (this.foreignKey.conditions && this.foreignKey.conditions.length > 0) {
        return concat(this.defaultCondition, this.foreignKey.conditions);
      } else {
        return this.defaultCondition;
      }
    },
    showPagination: function () {
      //let windowConfig = window.sessionStorage.getItem('childPagination') == true ||window.sessionStorage.getItem('childPagination') == false  ? window.sessionStorage.getItem('childPagination') : true
      // let isShow = this.childListConfig.showPagination
      let isShow = true;
      // console.log("showPagination",isShow)
      //console.log(isShow,this.storageType)
      if (isShow && this.storageType !== "mem") {
        return isShow == "false" || isShow == false ? false : true;
      } else {
        return false;
      }
    },
    serviceMoreConfig() {
      if (this.childListConfig?.more_config) {
        return JSON.parse(this.childListConfig?.more_config);
      } else if (this.childListConfig?._childMoreConfig) {
        return this.childListConfig._childMoreConfig;
      }
    },
    fkMoreConfig() {
      return this.foreignKey?.more_config
        ? JSON.parse(this.foreignKey?.more_config)
        : null;
    },
    uiMode() {
      return this.fkMoreConfig?.uiMode || this.serviceMoreConfig?.uiMode;
    },
    childListService() {
      let result = null;
      let mainData = this.mainFormDatas;
      if (this.serviceMoreConfig?.serviceMap) {
        Object.keys(this.serviceMoreConfig.serviceMap).forEach((service) => {
          if (this.serviceMoreConfig.serviceMap[service]) {
            if (eval(this.serviceMoreConfig.serviceMap[service]) === true) {
              result = service;
            }
          }
        });
      }
      if (!result) {
        result = this.service;
      }
      return result;
    },
    updatable() {
      if (this.foreignKey?.child_read_json) {
        const json = JSON.parse(this.foreignKey.child_read_json);
        return evalJson(json, this.mainFormDatas) !== true;
      }
    },
  },
  created: function () {},

  methods: {
    getInnerList() {
      return this.$refs?.list?.$refs?.list || this.$refs.list;
    },
    listLoaded(e) {
      // console.log("list Loaded",e)
      if (this.listType == "detaillist") {
        // console.log('detail child list loaded')
        this.$emit("detailOnLoaded", e);
      }
    },
    childDataLoadedRun(e) {
      console.log("child-list",e)
      this.$emit("child-loaded", e);
    },
    buildRunQuries() {
      if (this.$refs.mlist) {
        return this.$refs.mlist.buildMlistRunQuries(this.foreignKey) || [];
      } else if (this.$refs.frameList) {
        return this.$refs.frameList?.buildRunQuries(this.foreignKey) || [];
      } else {
        return this.getInnerList?.()?.buildRunQuries?.(this.foreignKey) || [];
      }
    },

    isInplaceEdit: function () {
      return this.getInnerList?.()?.isInplaceEdit?.();
      // return this.getInnerList?.()?.$refs?.list?.isInplaceEdit?.();
    },

    isMem: function () {
      let list = this.getInnerList();
      return list?.isMem?.();
    },

    buildExecutors4Edit: function () {
      let list = this.getInnerList();
      return list?.buildExecutors4Edit?.();
    },

    listrefresh: function () {
      let list = this.getInnerList();
      list?.loadTableData?.();
    },
    /**
     * hide ref column and set a vaue
     * @param form
     */
    onAddFormLoaded: function (form) {
      // hide ref cols
      let refCol =
        this.foreignKey.column_name || this.foreignKey.ref_service_column;
      let refColField = form.fields[refCol];
      refColField.info.visible = false;
      refColField.setSrvVal(this.getRefColValue);

      // hide extra fk condition cols and set values
      // 隐藏子表中跟主表关联的字段并给该字段设置默认值
      if (this.foreignKey.conditions) {
        this.foreignKey.conditions.forEach((item) => {
          let refCol = item.colName;
          let refColField = form.fields[refCol];
          refColField.info.visible = false;
          refColField.setSrvVal(item.value);
        });
      }

      form.actions.submit.nav2Location = null;

      this.$emit("add-form-loaded", form);
    },

    /**
     * hide ref column
     * @param form
     */
    onUpdateFormLoaded: function (form) {
      this.hiddRefColFieldInForm(form);
      form.actions.submit.nav2Location = null;

      this.$emit("update-form-loaded", form);
    },

    onFilterFormLoaded: function (form) {
      let refCol = this.foreignKey.column_name;
      refCol = "_" + refCol + "_disp";
      let refColField = form.fields[refCol];
      if (refColField) {
        refColField.info.visible = false;
      }
    },

    onInlineListLoaded: function (inlineList) {
      this.$emit("inline-list-loaded", inlineList, this);
    },
  },
};
</script>
